package org.basic.mail.controller;


import java.util.List;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.basic.mail.domain.SysMailAccount;
import org.basic.mail.domain.bo.SysMailTemplateBo;
import org.basic.mail.domain.bo.SysMailTemplateDialogBo;
import org.basic.mail.domain.vo.MailTemplateRespVo;
import org.basic.mail.domain.vo.MailTemplateSendReqVo;
import org.basic.mail.domain.vo.SysMailTemplateVo;
import org.basic.mail.enums.MailSSL;
import org.basic.mail.service.ISysMailAccountService;
import org.basic.mail.service.ISysMailTemplateService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.basic.common.idempotent.annotation.RepeatSubmit;
import org.basic.common.log.annotation.Log;
import org.basic.common.web.core.BaseController;
import org.basic.common.mybatis.core.page.PageQuery;
import org.basic.common.core.domain.R;
import org.basic.common.core.validate.AddGroup;
import org.basic.common.core.validate.EditGroup;
import org.basic.common.log.enums.BusinessType;
import org.basic.common.excel.utils.ExcelUtil;

import org.basic.common.mybatis.core.page.TableDataInfo;

/**
 * 邮件模版
 *
 * @author Shao Hua
 * @date 2023-07-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/mailTemplate")
public class SysMailTemplateController extends BaseController {

    private final ISysMailTemplateService sysMailTemplateService;

    private final ISysMailAccountService sysMailAccountService;
    /**
     * 查询邮件模版列表
     */
    @SaCheckPermission("system:mailTemplate:list")
    @GetMapping("/list")
    public TableDataInfo<SysMailTemplateVo> list(SysMailTemplateBo bo, PageQuery pageQuery) {
        return sysMailTemplateService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出邮件模版列表
     */
    @SaCheckPermission("system:mailTemplate:export")
    @Log(title = "邮件模版", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysMailTemplateBo bo, HttpServletResponse response) {
        List<SysMailTemplateVo> list = sysMailTemplateService.queryList(bo);
        ExcelUtil.exportExcel(list, "邮件模版", SysMailTemplateVo.class, response);
    }

    /**
     * 获取邮件模版详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:mailTemplate:query")
    @GetMapping("/{id}")
    public R<MailTemplateRespVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(sysMailTemplateService.queryById(id));
    }

    /**
     * 新增邮件模版
     */
    @SaCheckPermission("system:mailTemplate:add")
    @Log(title = "邮件模版", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysMailTemplateDialogBo bo) {
        return toAjax(sysMailTemplateService.insertByBo(bo));
    }

    /**
     * 修改邮件模版
     */
    @SaCheckPermission("system:mailTemplate:edit")
    @Log(title = "邮件模版", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysMailTemplateDialogBo bo) {
        return toAjax(sysMailTemplateService.updateByBo(bo));
    }

    /**
     * 删除邮件模版
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:mailTemplate:remove")
    @Log(title = "邮件模版", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(sysMailTemplateService.deleteWithValidByIds(List.of(ids), true));
    }

//    @SaCheckPermission("system:mailTemplate:send-mail")
    @PostMapping("/send-mail")
    public R<String> sendMail(@Valid @RequestBody MailTemplateSendReqVo sendReqVO) {
        return R.ok(sysMailTemplateService.sendMail(sendReqVO));

    }
}
